<template>
  <section class="todoapp">
    <TodoHeader @add="addFn"></TodoHeader>
    <TodoMain :arr="showList" @del="delFn"></TodoMain>
    <TodoFooter :arr="list" @chu="chuFn" @change="changeFn"></TodoFooter>
  </section>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
//样式
import "./styles/base.css";
import "./styles/index.css";

export default {
  data() {
    return {
      //默认展示所有数据
      type: "all",
      list: JSON.parse(localStorage.getItem("td25")) || [],
    };
  },

  //监听
  watch: {
    list: {
      deep: true, //深度监听
      handler() {
        localStorage.setItem("td25", JSON.stringify(this.list));
      },
    },
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    //头部添加
    addFn(name) {
      this.list.push({
        id: +new Date(),
        name,
        isDone: false,
      });
    },
    //中心内容删除
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    //删除已完成的任务
    chuFn() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    changeFn(type) {
      this.type = type;
    },
  },
  computed: {
    showList() {
      if (this.type === "active") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.type === "complete") {
        return this.list.filter((item) => item.isDone);
      } else {
        //计算所有的数据
        return this.list;
      }
    },
  },
};
</script>
